<?php
declare (strict_types=1);

namespace app\api\controller\v1;

use app\common\model\Datum;
use app\common\model\Record;
use app\common\model\Unit as UnitModel;

class Unit extends BaseController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $grade_id = input('grade_id/d', 0);     // 年级id
        $course_id = input('course_id/d', 0);   // 课程—类型id
        $model = new UnitModel();
        $where['status'] = 1;
        if ($grade_id > 0) $where['grade_id'] = $grade_id;
        if ($course_id > 0) $where['course_id'] = $course_id;
        $arr = $model->where($where)->field('id,title')->select()->toArray();
        foreach ($arr as $k => $v) {
            $where['unit_id'] = $v['id'];
            $arr[$k]['unit_total'] = Datum::where($where)->count();
            $arr[$k]['unit_work_total'] = Record::where($where)->where(['api_member_id' => $this->user_id])->count();
        }
        return sucessMsg(200, '获取成功', ['arr' => $arr]);
    }
}
